<?php


namespace App\Http\Controllers\merchant;

use App\Common\Components\ApiController;
use App\Models\Merchant\MerchantAdmin;
use App\Models\Merchant\MerchantAdminRoleRelation;
use App\Models\Merchant\MerchantRoleMenuRelation;
use App\Models\Merchant\MerchantRole;
use App\Models\Merchant\MerchantMenuRelation;
use App\Models\Merchant\MerchantMenu;
use Illuminate\Support\Facades\DB;

class AdminController extends ApiController
{
    /**
     * @OA\Get(
     *     path="/api/merchant/admin/list",
     *     tags={"商户管理员"},
     *     summary="获取商户管理员列表",
     *     description="分页获取商户管理员列表",
     *     @OA\Response(
     *         response=200,
     *         description="成功",
     *         @OA\JsonContent(
     *             @OA\Property(property="code", type="integer", example=200),
     *             @OA\Property(property="message", type="string", example="操作成功"),
     *             @OA\Property(property="data", type="object",
     *                 @OA\Property(property="current_page", type="integer", example=1),
     *                 @OA\Property(property="data", type="array", @OA\Items(
     *                     @OA\Property(property="id", type="integer", example=1),
     *                     @OA\Property(property="username", type="string", example="merchant_admin"),
     *                     @OA\Property(property="email", type="string", example="merchant@example.com"),
     *                     @OA\Property(property="status", type="integer", example=1)
     *                 )),
     *                 @OA\Property(property="total", type="integer", example=50)
     *             )
     *         )
     *     )
     * )
     */
    public function list(){
        $param = request()->input();
        $record = MerchantAdmin::paginate(10);
        return $this->successJson('操作成功',$record);
    }

    public function role(){
        $param = request()->input();
        $admin_id = $param['id'];
        $AdminRoleRelation = MerchantAdminRoleRelation::where('admin_id',$admin_id)->first();
        $record = [];
        if($AdminRoleRelation){
            $roleIds = explode(",", $AdminRoleRelation->role_ids);
            foreach($roleIds as $roleId){
            $record[] = MerchantRole::where('id',$roleId)->first();
        }
        }
        return $this->successJson('操作成功',$record);
    }

    public function updateStatus(){
        $param = request()->input();
        $admin_id = $param['id'];
        $status = $param['status'];
        $admin = MerchantAdmin::where('id',$admin_id)->first();
        $admin->status = $status;
        $admin->save();
        return $this->successJson();
    }

    public function updatePwd(){
        $param = request()->input();
        $admin_id = $param['id'];
        $password = $param['password'];
        $hash = password_hash($password, PASSWORD_DEFAULT);
        $admin = MerchantAdmin::where('id',$admin_id)->first();
        $admin->password = $hash;
        $admin->save();
        return $this->successJson();
    }

    public function update(){
        $param = request()->input();
        $admin_id = $param['id'];
        $admin = MerchantAdmin::where('id',$admin_id)->first();
        $admin->username = $param['username'];
        $admin->icon = $param['icon'];
        $admin->username = $param['username'];
        $admin->email = $param['email'];
        $admin->nick_name = $param['nick_name'];
        $admin->note = $param['note'];
        $admin->status = $param['status'];
        $admin->save();
        return $this->successJson();
    }

    public function login(){
        $param = request()->input();
        $username = $param['username'];
        $password = $param['password'];
        $admin = MerchantAdmin::where('username',$username)->first();
        $admin->login_time = now();
        $isPasswordCorrect = password_verify($password, $admin->password);
        if($isPasswordCorrect){
            $data['icon'] = $admin->icon;
            $data['username'] = $admin->username;
            $data['merchant_id'] = $admin->merchant_id;
            $admin->save();
            return $this->successJson('',$data);
        }
        return $this->errorJson();
    }


    public function register(){
        $param = request()->input();
        $admin = new MerchantAdmin();
        $admin->username = $param['username'];
        $admin->create_time = now();
        $record = MerchantAdmin::where('username',$admin->username)->first();
        if($record){
            return $this->errorJson('用户已存在');
        }
        $admin->fill($param);
        $admin->save();
        return $this->successJson();
    }

    public function delete(){
        $param = request()->input();
        $admin_id = $param['id'];
        MerchantAdmin::where('id', $admin_id)->delete();
        return $this->successJson();
    }

    public function roleUpdate(){
        $param = request()->input();
        $admin_id = $param['adminId'];
        $AdminRoleRelation = MerchantAdminRoleRelation::where('admin_id', $admin_id)->first();
        if($AdminRoleRelation){
            $AdminRoleRelation->role_ids = $param['roleIds'];
            $AdminRoleRelation->save();
        }else{
            MerchantAdminRoleRelation::create([
                'admin_id' => $admin_id,
                'role_ids' =>$param['roleIds'],
            ]);
        }
        return $this->successJson();
    }

    public function info(){
        $param = request()->input();
        $username = $param['username'];
        $Admin = MerchantAdmin::where('username',$username)->first();
        $AdminRoleRelation = MerchantAdminRoleRelation::where('admin_id', $Admin->id)->first();
        if(!$AdminRoleRelation){
            return $this->successJson();
        }
        $roleIds = explode(",", $AdminRoleRelation->role_ids);
        $menus = [];
        foreach($roleIds as $roleId){
            $RoleMenuRelation = MerchantRoleMenuRelation::where('role_id', $roleId)->first();
            if($RoleMenuRelation){
                $menuCodes = explode(",", $RoleMenuRelation->menu_code);
                $menus = array_merge($menus,$menuCodes);
            }
        }
        //查出未授权的菜单
        $MerchantMenuRelation = MerchantMenuRelation::where('merchant_id',$Admin->merchant_id)->first();
        $noMenus = [];
        if($MerchantMenuRelation){
            $menu_code = explode(",", $MerchantMenuRelation->menu_code);
            $MerchantMenu = MerchantMenu::get();
            foreach($MerchantMenu as $item){
                if(!in_array($item->menu_code, $menu_code)){
                    $noMenus[] = $item->menu_code;
                }
            }
        }
        //过滤掉未授权的菜单
        // if($noMenus){
        //     $menus = array_diff($menus, $noMenus);
        // }
        $data['noMenus'] = $noMenus;
        $data['roles'] = $roleIds;
        $data['menus'] = $menus;
        $data['username'] = $username;
        $data['user'] = $Admin;
        return $this->successJson('',$data);
    }
}
